package com.example.myproject.service.impl;

import com.example.myproject.database.primary.entity.User1Entity;
import com.example.myproject.database.primary.repository.UserRepositoryPrimary;
import com.example.myproject.database.secondary.entity.User2Entity;
import com.example.myproject.database.secondary.repository.UserRepositorySecondary;
import com.example.myproject.service.UserService;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
//import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserRepositoryPrimary userRepositoryPrimary;

    @Autowired
    private UserRepositorySecondary userRepositorySecondary;

    @Override
    public void FindAllUser2AndInsertAll1() {
        //List<User1Entity> user1EntityList = userRepositoryPrimary.findAll();
        List<User2Entity> user2EntityList = userRepositorySecondary.findAll();

        if(null == user2EntityList || user2EntityList.isEmpty()) {
            logger.info("数据记录条目不能为空");
            return;
        }

        logger.info("----------开始记录----------数据条目:{}", user2EntityList.size());
        // 查询重复数据
        List<User2Entity> user2EntityInDb = Lists.newArrayList();
        for(User2Entity user2Entity: user2EntityList) {
            User1Entity ue = userRepositoryPrimary.getOne(user2Entity.getId());
            //User1Entity ue = findByCardNo(userEntity.getCardNo());
            if(null != ue) {
                /*
                if(ue.equals(user2Entity)) {

                } else {

                }*/
                user2EntityInDb.add(user2Entity);
            }
        }
        // 去掉重复数据
        user2EntityList.removeAll(user2EntityInDb);

        List<User1Entity> user1EntityList =  Lists.newArrayList();
        for(User2Entity user2Entity : user2EntityList) {
            User1Entity user1Entity = new User1Entity();
            user1Entity.setId(user2Entity.getId());
            user1Entity.setName(user2Entity.getCustName());
            user1Entity.setEmail(user2Entity.getCustEmail());
            user1Entity.setDisabled(false);
        }
        userRepositoryPrimary.save(user1EntityList);
        logger.info("----------记录结束----------数据条目:{}", user2EntityList.size());
    }

}
